package com.gupao;

/**
 * 模拟多线程原子性问题
 * Created by WDJ on 2019/11/27.
 */
public class AtomicDemo {

    public volatile static int count = 0;

    public static void incr(){
        try {
            Thread.sleep(1);
            count++;
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        for (int i = 0; i < 1000; i++) {
            new Thread(AtomicDemo::incr).start();
        }
        try {
            Thread.sleep(4000);  //确保1000个线程执行完毕
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("count:"+count);
    }

}
